package cn.yedong.org.service.impl;
/**
 * 业务实现类：部门
 */

import cn.yedong.basic.service.Impl.BaseServiceImpl;
import cn.yedong.basic.util.Md5Utils;
import cn.yedong.basic.util.StrUtils;
import cn.yedong.org.domain.Employee;
import cn.yedong.org.mapper.EmployeeMapper;
import cn.yedong.org.service.IEmployeeService;
import cn.yedong.user.domain.Logininfo;
import cn.yedong.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;


    /**
     * 同步登陆信息：添加
     * 再添加员工之前，要添加员工的登陆信息
     * 添加员工时，要添加员工的角色
     * @param employee
     */
    @Override
    public void add(Employee employee) {
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(1);
        logininfo.setDisable(true);//后台直接添加默认为启动
        logininfoMapper.add(logininfo);//产生自增id

        employee.setLogininfo_id(logininfo.getId());
        employeeMapper.add(employee);
        super.add(employee);

        //添加员工时，添加员工的角色
        if(employee.getRole_id() != null){
            employeeMapper.addRole(employee);
        }
    }

    /**
     * 同步登录信息：删除
     * 删除员工之前先删除登陆信息
     * 删除员工之前先删除其角色
     * @param id
     */
    @Override
    public void del(Long id) {
        Employee dbEmployee = employeeMapper.loadById(id);
        logininfoMapper.del(dbEmployee.getLogininfo_id());

        //删除角色
        employeeMapper.delRoleByEmployeeId(id);
        super.del(id);
    }

    /**
     * 登陆信息同步：修改
     * 修改员工前，先修改其登陆信息
     * @param employee
     */
    @Override
    public void update(Employee employee) {
        Employee dbEmployee = employeeMapper.loadById(employee.getId());
        Logininfo dbLogininfo = logininfoMapper.loadById(dbEmployee.getLogininfo_id());

        //如果密码改动了就要修改
        if(!dbEmployee.getPassword().equals(employee.getPassword())){
            //处理盐值和密码
            String salt = StrUtils.getComplexRandomString(32);
            String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
            employee.setSalt(salt);
            employee.setPassword(md5Pwd);
        }

        BeanUtils.copyProperties(employee,dbLogininfo);
        //拷贝属性之后，id会被覆盖
        dbLogininfo.setId(dbEmployee.getLogininfo_id());
        //如果员工状态修改，登录信息Disable也修改
        if(employee.getState() == 1){
            dbLogininfo.setDisable(true);
        }else {
            dbLogininfo.setDisable(false);
        }
        logininfoMapper.update(dbLogininfo);

        //删除角色
        employeeMapper.delRoleByEmployeeId(employee.getId());
        //添加员工时，添加员工的角色
        if(employee.getRole_id() != null){
            employeeMapper.addRole(employee);
        }

        super.update(employee);
    }

    /**
     * 同步登陆信息：批量删除
     * @param ids
     */
    @Override
    public void patchDel(List<Long> ids) {
        for (Long id : ids) {
            del(id);
        }
//        super.patchDel(ids);
    }
}
